#!/usr/bin/env python

###########################################################################
#  Copyright 2010 Jim Pulokas
#
#  This file is part of focusfun.
#  
#  focusfun is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  
#  focusfun is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with focusfun.  If not, see <http://www.gnu.org/licenses/>.
###########################################################################

import Image
import glob
import numpack

def range_diff(prefix):
	files = glob.glob('%s-???-???.png' % (prefix,))
	files.sort()
	file0 = files[0]
	im0 = Image.open(file0)
	im0 = numpack.image_to_array(im0)
	im0 = numpack.to_float(im0)
	for i,file in enumerate(files[1:]):
		file1 = file
		im1 = Image.open(file1)
		im1 = numpack.image_to_array(im1)
		im1 = numpack.to_float(im1)
		diff = numpack.num.absolute(im1 - im0)
		diffim = numpack.array_to_image(diff)
		diffim.save('%s-diff-%03d.png' % (prefix, i))
		diff = numpack.num.sum(numpack.num.ravel(diff))
		print diff, file0, file1
		file0 = file1
		im0 = im1

if __name__ == '__main__':
	import sys
	prefix = sys.argv[1]
	range_diff(prefix)
